package qumaimai.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import qumaimai.dao.AddressDao;
import qumaimai.dao.impl.AddressDaoImpl;
import qumaimai.entity.Address;
import qumaimai.entity.User;
import qumaimai.entity.vo.CartVo;

@WebServlet("/AddressServlet")
public class AddressServlet extends BaseServlet{
	AddressDao dao = new AddressDaoImpl();
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		List<Address> list = dao.findAll(user.getId());
    	session.setAttribute("AddressList", list);
    	
		response.sendRedirect("address.jsp");
	}
	
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		String receiver = request.getParameter("receiver");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String part = request.getParameter("part");
		
		Address addr = new Address(user.getId(), province, city, district, address, remark, receiver, phone);
		
		int n = dao.add(addr);
		if(n>0){
			findAll(request, response);
		}
	}
	
	public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		int id = Integer.parseInt(request.getParameter("id"));
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String district = request.getParameter("district");
		String address = request.getParameter("address");
		String remark = request.getParameter("remark");
		String receiver = request.getParameter("receiver");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String part = request.getParameter("part");
		
		Address addr = new Address(id, user.getId(), province, city, district, address, null, 0, remark, receiver, email, phone, part, null);
		
		int n = dao.edit(addr);
		if(n>0){
			findAll(request, response);
		}
	}
	
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Address address = dao.findById(id);
		String json = JSON.toJSONString(address);
		response.getWriter().print(json);
	}
	
	public void change(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		int id = Integer.parseInt(request.getParameter("id"));
		Address address = dao.findById(id);
		System.out.println(address);
		session.setAttribute("defaultAddress", address);
		response.sendRedirect("buycartwo.jsp");
	}
	
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		int n = dao.del(id);
		if(n>0){
			findAll(request, response);
		}
		
	}
	
	public void setDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int id = Integer.parseInt(request.getParameter("id"));
		
		dao.clearDefault(user.getId());
		int n = dao.setDefault(id);
		if(n>0){
			findAll(request, response);
		}
		
	}
}
